	.INCLUDE	"p33FJ16GS502.INC"
	.INCLUDE 	"MACRO.INC"
	
	.GLOBAL		FZY
	.GLOBAL		DT
	.GLOBAL 	T1	; T(n)
	.GLOBAL		T2	; T(n+1)

.SECTION	.XBSS, BSS, XMEMORY

; FZY BUFS	
	R_FLAG:	.SPACE	2
	T1:		.SPACE	2
	T2:		.SPACE	2
	T_RANG:	.SPACE	2

	.EQU	TS,		-1
	.EQU	TMS,	0
	.EQU	TM,		1
	.EQU	TML,	2
	.EQU	TL,		3
	
	.EQU	AT_TM,	0X0010	; 0000 0000 0001 0000
	.EQU	R_TM,	0X000F	; 0000 0000 0000 1111
	.EQU	L_TM,	0X01E0	; 0000 0001 1110 0000
	.EQU	AT_TML,	0X0004	; 0000 0000 0000 0100
	.EQU	R_TML,	0X0003	; 0000 0000 0000 0011
	.EQU	TM_TML,	0X0008	; 0000 0000 0000 1000
	.EQU	ATR_TL,	0X0001	; 0000 0000 0000 0001
	.EQU	TML_TL,	0X0002	; 0000 0000 0000 0010
	.EQU	AT_TMS,	0X0040	; 0000 0000 0100 0000
	.EQU	TMS_TM,	0X0020	; 0000 0000 0010 0000
	.EQU	L_TMS,	0X0180	; 0000 0001 1000 0000
	.EQU	ATL_TS,	0X0100	; 0000 0001 0000 0000
	.EQU	TS_TMS,	0X0080	; 0000 0000 1000 0000
	
FZY_INITIAL:

	CLR	R_FLAG
	CLR	T_RANG
	CLR	T1
	CLR	T2
	
	SUB T2, T1, DT
	
	RETURN
	
	
A_SEP:		; NOT KNOWING WHERE

	SUB		DT,TM,T_RANG	; DT-TM=T_RANG
	MOVFF	T_RANG,W0		; T_RANG=W0
	MOVFF	TM, W14			; TM=W14
	CPSEQ	W0,W14			; SKIP IF W0==W14, IN TM
	GOTO	A_SEP_LR
	MOVFF	AT_TM,R_FLAG	; RANG: AT TM
	RETURN	
	
A_SEP_LR:	; NOT AT TM, ANY ELSE IS POSSIBLE

	CPSGT	W0,W14			; SKIP IF W0>W14 
	GOTO	C_SEP			; RANG: LEFT OF TM
	MOVFF	R_TM,R_FLAG		; RANG: RIGHT OF TM
	GOTO	B_SEP
B_SEP:		; FROM RIGHT OF TM

	SUB		DT,TML,T_RANG	; DT-TML=T_RANG
	MOVFF	T_RANG,W0		; T_RANG=W0
	MOVFF	TM, W14			; TM=W14
	CPSEQ	W0,W14			; SKIP IF W0==W14, IN TML
	GOTO	B_SEP_LR
	MOVFF	AT_TML,R_FLAG	; RANG: AT TML
	;SHOULD BE A VARIABLE TO DECIDE GRADITUDE
	RETURN	

B_SEP_LR:
	CPSGT	W0,W14			; SKIP IF W0>W14 
	MOVFF	L_TML,R_FLAG	; IF W0!>W14 THEN W0<W14, RANG: LEFT OF TML
	MOVFF	R_TML,R_FLAG	; RANG: RIGHT OF TML
C_SEP: 		; FROM LEFT OF TM
	MOVFF	L_TM,R_FLAG		; IF W0!>W14 THEN W0<W14, RANG: LEFT OF TM
	SUB		DT,TML,T_RANG	; DT-TML=T_RANG
	MOVFF	T_RANG,W0		; T_RANG=W0
	MOVFF	TM, W14			; TM=W14
	CPSEQ	W0,W14			; SKIP IF W0==W14, IN TML
	GOTO	B_SEP_LR
	MOVFF	AT_TML,R_FLAG	; RANG: AT TML
	RETURN	
	
	CPSNE	R_FLAG,R_TM	; SKIP IF R_FLAG!=R_TM
	
	